package site.edody.dframe.websocket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * DFrame WebSocket 配置类
 * @author Dody
 */
@Configuration
public class DFrameWebSocketConfigure {

    private static final Logger log = LoggerFactory.getLogger(DFrameWebSocketConfigure.class);

    @Bean
    @ConfigurationProperties(prefix = "dframe.websocket")
    public DFrameWebSocketProperties dFrameWebSocketProperties() {
        log.info("创建 DFrameWebSocketProperties Bean");
        return new DFrameWebSocketProperties();
    }

    @Bean
    public DFrameWebSocketManager dFrameWebSocketManager(
            DFrameWebSocketProperties webSocketProperties,
            DFrameWebSocketSessionCache dFrameWebSocketSessionCache,
            DFrameWebSocketDataCache dFrameWebSocketDataCache,
            DFrameDefaultWebSocketListener dFrameDefaultWebSocketListener) {

        log.info("创建 DFrameWebSocketManager Bean");
        log.info("注入的 webSocketProperties: {}", webSocketProperties);
        log.info("servers 配置: {}", webSocketProperties.getServers());

        return new DFrameWebSocketManager(webSocketProperties, dFrameWebSocketSessionCache, dFrameWebSocketDataCache, dFrameDefaultWebSocketListener);
    }

    @Bean
    public DFrameWebSocketSessionCache dFrameWebSocketSessionCache() {
        log.info("创建 DFrameWebSocketSessionCache Bean");
        return new DFrameWebSocketSessionCache();
    }

    @Bean
    public DFrameWebSocketDataCache dFrameWebSocketDataCache() {
        log.info("创建 DFrameWebSocketDataCache Bean");
        return new DFrameWebSocketDataCache();
    }

    @Bean
    public DFrameDefaultWebSocketListener dFrameDefaultWebSocketListener() {
        log.info("创建 DFrameDefaultWebSocketListener Bean");
        return new DFrameDefaultWebSocketListener();
    }

    @PostConstruct
    public void init() {
        log.info("DFrameWebSocketConfigure 初始化完成");
    }
}